package com.renjun.smweb.commons.global;

import com.renjun.smweb.commons.util.JdbcProperties;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Created by root on 16-11-7.
 */
public class DBCPConnectionPool {

    private volatile static DBCPConnectionPool uniqueInstance;

    private static String masterConfig = "test93_master.properties";

    private static String sbConfig = "test93_sb.properties";

    private static String jssbConfig = "test93_jssb.properties";

    private static String xfConfig = "test93_xf.properties";

    private DataSource masterDs = null;

    private DataSource sbDs = null;

    private DataSource xfDs = null;

    private DataSource jssbDs = null;

    private Properties createProperties(int type){
        Properties properties = new Properties();
        InputStream in = null;
        try {
            switch (type){
                default:break;
                case 0:
                    in = DBCPConnectionPool.class.getClassLoader().getResourceAsStream(masterConfig);
                    properties.load(in);
                    break;
                case 1:
                    in = DBCPConnectionPool.class.getClassLoader().getResourceAsStream(sbConfig);
                    properties.load(in);
                    break;
                case 2:
                    in = DBCPConnectionPool.class.getClassLoader().getResourceAsStream(xfConfig);
                    properties.load(in);
                    break;
                case 3:
                    in = DBCPConnectionPool.class.getClassLoader().getResourceAsStream(jssbConfig);
                    properties.load(in);
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(in!=null)in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return properties;
    }

    public static DBCPConnectionPool getUniqueInstance(){
        if(uniqueInstance==null){
            synchronized (DBCPConnectionPool.class){
                if(uniqueInstance==null){
                    uniqueInstance = new DBCPConnectionPool();
                }
            }
        }
        return uniqueInstance;
    }

    private DBCPConnectionPool(){
        init();
    }

    public Connection getMasterConnection() throws SQLException {
        return masterDs.getConnection();
    }

    public Connection getSbConnection() throws SQLException {
        return sbDs.getConnection();
    }

    public Connection getJSSbConnection() throws SQLException {
        return jssbDs.getConnection();
    }

    public Connection getXfConnection() throws SQLException {
        return xfDs.getConnection();
    }

    private void init(){
        loadDBCPconnectionPool();
        Properties mp = createProperties(0);
        Properties sp = createProperties(1);
        Properties xp = createProperties(2);
        Properties jssp = createProperties(3);
        try {
            masterDs = BasicDataSourceFactory.createDataSource(mp);
            sbDs = BasicDataSourceFactory.createDataSource(sp);
            xfDs = BasicDataSourceFactory.createDataSource(xp);
            jssbDs = BasicDataSourceFactory.createDataSource(jssp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void loadDBCPconnectionPool(){
        masterConfig = JdbcProperties.DBCPCONPOOL_MASTERCONFIG;
        sbConfig = JdbcProperties.DBCPCONPOOL_SBCONFIG;
        jssbConfig = JdbcProperties.DBCPCONPOOL_JSSBCONFIG;
        xfConfig = JdbcProperties.DBCPCONPOOL_XFCONFIG;
    }
}
